package com.example.springbootbook2.controller;

import com.example.springbootbook2.model.Result;
import com.example.springbootbook2.model.UserInfo;
import com.example.springbootbook2.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Result<String> login(UserInfo userInfo, HttpServletRequest request) {
      log.info("用户登录userinfo{}",userInfo);
      String result = userInfoService.login(userInfo);
      if ("".equals(result)) {
          HttpSession session = request.getSession();
          session.setAttribute("userInfo",userInfo.getUserName());
          return Result.success("");
      }
      return Result.fail(result);
    }
}
